﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using Flatland.GeneticProgramming.CoreTypes;

/**
* 
* Copyright (c) 2012 Anders Høst Kjærgaard | ahkj@itu.dk
* 
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
* associated documentation files (the "Software"), to deal in the Software without restriction, 
* including without limitation the rights to use, copy, modify, merge, publish, distribute, 
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial 
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT 
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

namespace Flatland.GeneticProgramming.Examples {
    /// <summary>
    /// Template for a GenoType implementation. It shows what member must at least be implemented for the template to be valid marked with.
    /// Examples can be found in the Examples namespace
    /// </summary>
    [Serializable]
    public class StringExample : GenoType, IExpression<String>
    {

        public static string Answer = "flatland";
        
        [Primitive] 
        public String[] letters = new []{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", ""};

     //   [Primitive]
     //   public String[] nice_letters = new[] { "a", "d", "f", "l", "n", "t" };

        [Primitive]
        public String Concat(String a, String b){
            return String.Concat(a, b);
        }

        public override double Fitness()
        {
            int error = 0;
            string guess = (String)Execute();
            error += Math.Abs(Answer.Length - guess.Length) * 100;
            for (int i = 0; i < Answer.Length; i++){
                if(i < guess.Length)
                {
                    error += Math.Abs(guess[i] - Answer[i]);
                }
            }

            return GP.DEFAULT_FITNESS_GOAL - error;
        }


        /// <summary>
        /// Called when this genotype should act for the Agent.
        /// </summary>
        /// <param name="msg"></param>
        protected override void Act(ref string msg) {
            /************* Execute your the program with the Agent in Flatland ******************
             * 
             * Execute();
             * 
             */
        }

        /// <summary>
        /// Reports are clonable.
        /// TODO: Must be implemented</summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new StringExample();
        }

        /// <summary>
        /// Reports are deserialized and this constructor must be provided. 
        /// Consider implementing GetObjectData</summary>
        /// <param name="info"></param>
        /// <param name="context"></param>
        public StringExample(SerializationInfo info, StreamingContext context) {
        }

        
        /// <summary>
        /// Reports must expose a public parameterless constructor.
        /// </summary>
        public StringExample() { }
    }
}

